<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Services\QuestionService;
use App\Services\OSS;
use Illuminate\Http\Request;
use Session;

class FeedBackController extends Controller{
    public function upload(Request $request){

        if(!intval($_POST['member_id'])){
            $_data = array('code'=>'3x000001','msg'=>'请登录');
        }else{
            $file_dir = $_POST['file'];
            $file =$request->file('images');

            if(!$file->isValid()){
                $_data = array('code'=>400,'暂无文件上传');
            }else{
                // 获取扩展名
                $ext = $file->getClientOriginalExtension();

                // 生成新文件名
                $newfile = md5(time().rand(1000,9999)).'.'.$ext;

                $target_file = $file_dir.'/'.intval($_POST['member_id']).'/'.$newfile;

                $res = OSS::upload($target_file,$file->getRealPath());

                if($res){
                    $_data = array('code'=>'2x000001','source_src'=>$target_file);
                }else{
                    $_data = array('code'=>'4x000001','msg'=>'上传失败');
                }
            }
        }

        return response()->json($_data);
    }

	public function store(){
		$question = new QuestionService;

        date_default_timezone_set('PRC');
        $data['member_id'] = intval($_POST['member_id']);
        $data['content'] = addslashes($_POST['content']);
        $data['imgurl'] = $_POST['imgurl'] ? json_encode($_POST['imgurl']) : '';
        $data['isshow'] = 1;
        $data['add_date'] = date('Y-m-d H:i:s');

        $info = $question->getQuestionInfoByName($data['content']);
        if(!empty($info)){
            $_data = array('code'=>'3x000002','msg'=>'您已反馈过该问题');
        }else{
            $res = $question->insertQuestion($data);
            if($res){
                $_data = array('code'=>'2x000001','msg'=>'问题反馈成功');
            }else{
                $_data = array('code'=>'4x000001','msg'=>'问题反馈失败');
            }
        }

		return response()->json($_data);
	}
}